﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLL.Department
{
	/// <summary>
	/// 单位控制类
	/// </summary>
	/// <![CDATA[2014-8-11 16:06:37]]>
	public class Component : DLL.ComponentBase
	{
		#region 属性
		/// <summary>
		/// 问题数据类对象
		/// </summary>
		/// <![CDATA[2014-8-11 16:06:37]]>
		private Database clsDatabase;		// 数据类对象
		/// <summary>
		/// 配置类对象
		/// </summary>
		/// <![CDATA[2014-8-11 16:06:37]]>
		private Config clsConfig = new Config();		// 配置类对象
		#endregion 属性

		#region 构造函数
		/// <summary>
		/// 控制类构造函数
		/// </summary>
		/// <![CDATA[2014-8-11 16:06:37]]>
		public Component(System.Web.UI.Page page)
			: base(page)
		{
			clsDatabase = new Database(page);

			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nID", clsDatabase.ID, clsDatabase.listStructure[clsDatabase.ID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nStatus", clsDatabase.STATUS, clsDatabase.listStructure[clsDatabase.STATUS].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strDepartmentName", clsDatabase.NAME, clsDatabase.listStructure[clsDatabase.NAME].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nParentDepartmentID", clsDatabase.PARENTID, clsDatabase.listStructure[clsDatabase.PARENTID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nDepartmentGrade", clsDatabase.GRADE, clsDatabase.listStructure[clsDatabase.GRADE].strName));

			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nID", clsDatabase.listStructure[clsDatabase.ID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nStatus", clsDatabase.listStructure[clsDatabase.STATUS].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strDepartmentName", clsDatabase.listStructure[clsDatabase.NAME].strName, "LIKE"));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nParentDepartmentID", clsDatabase.listStructure[clsDatabase.PARENTID].strName, "="));
		}
		#endregion 构造函数

		#region 类型转换
		protected override TonyNet4V1.EntityBase GetEntity()
		{
			return new Entity();
		}
		protected override TonyNet4V1.QueryEntityBase GetQuery()
		{
			return new QueryEntity();
		}
		protected override TonyNet4V1.DataBase GetData()
		{
			return new Database(page);
		}
		/// <summary>
		/// 把查询结果链表中的实体类型转为现在的实体类型
		/// </summary>
		/// <![CDATA[2014-8-11 16:06:37]]>
		/// <param name="list"></param>
		/// <returns></returns>
		private List<Entity> ConvertListEntityType(List<TonyNet4V1.EntityBase> list)
		{
			List<Entity> listEntity = new List<Entity>();

			for (int i = 0; i < list.Count; i++)
			{
				Entity clsEntity = new Entity();

				clsEntity = (Entity)list[i];

				listEntity.Add(clsEntity);
			}

			return listEntity;
		}
		#endregion 类型转换

		#region 添加、修改等
		/// <summary>
		/// 添加
		/// </summary>
		/// <![CDATA[2014-8-11 16:58:36]]>
		/// <param name="clsEntity"></param>
		/// <returns></returns>
		public override bool Add(TonyNet4V1.EntityBase clsEntity)
		{
			Entity clsAddDepartment = new Entity();
			Entity clsParentDepartment = new Entity();

			clsAddDepartment = (Entity)clsEntity;
			clsParentDepartment = GetOne(clsAddDepartment.ParentDepartmentID);
			if (clsParentDepartment != null)
			{
				clsAddDepartment.DepartmentGrade = clsParentDepartment.DepartmentGrade + 1;
			}
			else
			{
				clsAddDepartment.DepartmentGrade = 1;
			}

			return base.Add(clsEntity);
		}
		/// <summary>
		/// 修改
		/// </summary>
		/// <![CDATA[2014-8-11 18:03:58]]>
		/// <param name="clsEntity"></param>
		/// <returns></returns>
		public override bool Modify(TonyNet4V1.EntityBase clsEntity)
		{
			Entity clsNewDepartment = new Entity();
			Entity clsOldDepartment = new Entity();
			List<Entity> list = new List<Entity>();
			bool bReturn = false;

			clsNewDepartment = (Entity)clsEntity;
			clsOldDepartment = GetOne(clsNewDepartment.ID);

			//if (clsNewDepartment.ParentDepartmentID == clsOldDepartment.ParentDepartmentID)
			//{
				//return base.Modify(clsNewDepartment);
			//}
			//else
			//{
				Entity clsParentDepartment = new Entity();

				clsParentDepartment = GetOne(clsNewDepartment.ParentDepartmentID);
				if (clsParentDepartment != null)
				{
					clsNewDepartment.DepartmentGrade = clsParentDepartment.DepartmentGrade + 1;
				}
				else
				{
					clsNewDepartment.DepartmentGrade = 1;
				}
				bReturn = base.Modify(clsNewDepartment);

				if (bReturn)
				{
					list = QueryByParent(clsNewDepartment.ID);

					for (int i = 0; i < list.Count; i++)
					{
						if (Modify(list[i]))
						{
						}
						else
						{
							bReturn = false;
						}
					}
				}

				return bReturn;
			//}
			
		}
		#endregion 添加、修改等

		#region 查询
		/// <summary>
		/// 根据ID获得实体对象
		/// </summary>
		/// <![CDATA[2014-8-11 16:06:37]]>
		/// <param name="nID"></param>
		/// <returns></returns>
		public new Entity GetOne(int nID)
		{
			return (Entity)base.GetOne(nID);
		}

		/// <summary>
		/// 查询所有
		/// </summary>
		/// <![CDATA[2014-8-11 16:06:37]]>
		/// <returns></returns>
		public new List<Entity> QueryAll()
		{
			return ConvertListEntityType(base.QueryAll());
		}

		/// <summary>
		/// 查询
		/// </summary>
		/// <![CDATA[2014-8-11 16:06:37]]>
		/// <returns></returns>
		public List<Entity> Query(QueryEntity clsQuery)
		{
			return ConvertListEntityType(base.Query(clsQuery));
		}
		#endregion 查询

		/// <summary>
		/// 查询全部单位，并按照单位级别排序
		/// </summary>
		/// <![CDATA[2014-8-11 17:51:50]]>
		/// <returns></returns>
		public List<Entity> QueryAllOrder()
		{
			List<Entity> list = new List<Entity>();

			QueryByParentOrder(clsConfig.nDepartment_NoParent, list);

			return list;
		}
		/// <summary>
		/// 根据上级单位ID查询
		/// </summary>
		/// <![CDATA[2014-8-11 23:17:36]]>
		/// <returns></returns>
		public List<Entity> QueryByParent(int nParentDepartmentID)
		{
			QueryEntity clsQuery = new QueryEntity();

			clsQuery.nParentDepartmentID = nParentDepartmentID;

			return Query(clsQuery);
		}

		/// <summary>
		/// 查询本单位所有下级单位的ID字符串
		/// </summary>
		/// <param name="nDepartmentID">单位ID</param>
		/// <returns>下级所有单位ID组成的字符串</returns>
		public string QueryChildDepartmentID(int nDepartmentID)
		{
			List<Entity> list = new List<Entity>();
			string strReturn = "";

			QueryByParentOrder(nDepartmentID, list);

			strReturn = nDepartmentID.ToString();

			for (int i = 0; i < list.Count; i++)
			{
				if (strReturn != "")
				{
					strReturn = strReturn + ", " + list[i].ID.ToString();
				}
				else
				{
					strReturn = list[i].ID.ToString();
				}
			}

			return strReturn;
		}

		/// <summary>
		/// 根据上级单位ID查询下级单位，把结果添加到list中
		/// </summary>
		/// <![CDATA[2014-8-11 17:04:32]]>
		/// <param name="nParentDepartmentID"></param>
		/// <param name="list"></param>
		private void QueryByParentOrder(int nParentDepartmentID, List<Entity> list)
		{
			List<Entity> listQuery = new List<Entity>();
			QueryEntity clsQuery = new QueryEntity();

			clsQuery.nParentDepartmentID = nParentDepartmentID;

			listQuery = Query(clsQuery);
			for (int i = 0; i < listQuery.Count; i++)
			{
				list.Add(listQuery[i]);

				QueryByParentOrder(listQuery[i].ID, list);
			}
		}

		/// <summary>
		/// 把单位链表根据各个单位等级，在单位名称前加上对应等级的符号
		/// </summary>
		/// <![CDATA[2014-8-11 17:55:52]]>
		/// <param name="list"></param>
		/// <param name="strFirstMark"></param>
		/// <param name="strGradeMark"></param>
		public void AddGradeInName(List<Entity> list, string strFirstMark, string strGradeMark)
		{
			string strMark = "";

			for (int i = 0; i < list.Count; i++)
			{
				Entity clsDepartment = list[i];
				strMark = "";
				for (int j = 1; j < clsDepartment.DepartmentGrade; j++)
				{
					strMark = strMark + strGradeMark;
				}
				if (clsDepartment.DepartmentGrade > clsConfig.nColumn_FirstGrade)
				{
					clsDepartment.DepartmentName = strFirstMark + strMark + "  " + clsDepartment.DepartmentName;
				}
				else
				{
					clsDepartment.DepartmentName = strMark + "  " + clsDepartment.DepartmentName;
				}
			}
		}
	}
}
